package com.servlet.form;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.base.Contact;
import com.servlet.model.base.Experience;
import com.servlet.model.base.Sector;
import com.servlet.model.base.Study;
import com.servlet.model.database.BaseAcces;

public class ModifProForm extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		boolean correct = false;

		HttpSession session = request.getSession();
		//BaseAcces base = (BaseAcces) this.config.getServletContext().getAttribute("base");
		
		String email = (String) session.getAttribute("email");
		
		BaseAcces base = ContextListener.base;		
		
		String societe = request.getParameter("societe");
		String domaine = request.getParameter("domaine");
		String moisDebut = request.getParameter("moisDebut");
		String anneeDebut = request.getParameter("anneeDebut");
		String moisFin = request.getParameter("moisFin");
		String anneeFin = request.getParameter("anneeFin");

		String poste = request.getParameter("poste");
		String description = request.getParameter("description");

		String cityCode = request.getParameter("cityCode");
		String pays = request.getParameter("pays");
		String ville = request.getParameter("ville");
		
		String resultat = null;

		int moisDebutInt = Integer.parseInt(moisDebut);
		if(moisDebutInt <10 && moisDebutInt > 0)
		{
			moisDebut = "0"+moisDebutInt;
		}
		
		int moisFinInt = Integer.parseInt(moisFin);
		if(moisFinInt <10 && moisFinInt > 0)
		{
			moisFin = "0"+moisFinInt;
		}
		
		Date dateDebut = Date.valueOf(anneeDebut + "-" + moisDebut + "-" + "01");
		Date dateFin = Date.valueOf(anneeFin + "-" + moisFin + "-" + "01");
		
		if( societe != null && societe != "" && cityCode != null && cityCode != "" && pays != null && pays != "" && ville != null && ville != "" && poste != null && poste != "" )
		{
			if( Format.verifyName(societe) && Format.verifyName(pays) && Format.verifyName(ville) && Format.verifyName(poste))
			{
				if( Format.verifyNumero(cityCode) )
				{
					if( dateDebut.before(dateFin) )
					{
						correct = true;
					}
					else
					{
						resultat = "Echec du une mauvaise chronologie";
					}
				}
				else
				{
					resultat = "Echec du au city Code";
				}
			}
			else
			{
				resultat = "Echec du a un champ mal rempli";
			}
		}
		else
		{
			resultat = "Echec du a un champ non rempli";
		}

		if( correct )
		{
			System.out.println("Creation experience : " + societe + " " + domaine + " " + dateDebut + "  " +  dateFin + " " + poste + " " + description + " " + cityCode + " "+  pays + " "+  ville );

			Experience e = (Experience) session.getAttribute("experience");
			
			base.removeExperience(e);
			
			base.addExperience(email, societe, dateDebut, dateFin, poste, new Sector( domaine , true ) , description, Integer.parseInt(cityCode) , pays , ville );
			
			session.setAttribute("resultat", "Creation reussie !! ");
			
			ArrayList<Contact> a = base.findContacts(email);
			
			java.util.Date dd = Calendar.getInstance().getTime();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String dat = dateFormat.format(dd);
			Date dateActuelle = Date.valueOf(dat);
			
			for( int i = 0 ; i < a.size() ; i++ )
			{
				if( a.get(i).getIs_accepted() )
				if( a.get(i).getEmail_user1().compareTo(email) == 0 )
					base.addNews( email , a.get(i).getEmail_user2() , dateActuelle , 0 );
				else
					base.addNews( email , a.get(i).getEmail_user1() , dateActuelle , 0 );
			}
		}
		else
		{
			System.out.println(resultat);
			
			session.setAttribute("resultat", "Creation ratée... désolé !! \n Raison : " + resultat );
		}
		
		response.sendRedirect("profil");
	}
}

